package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
)

type ArrangesRBody struct {
	Data       []models.Arrange `json:"data"`
	PageNo     int              `json:"pageNo"`
	PageSize   int              `json:"pageSize"`
	TotalCount int64            `json:"totalCount"`
	TotalPage  int64            `json:"totalPage"`
}
type ArrangesBody struct {
	State    int64 `json:"state"`
	PageNo   int   `json:"pageNo"`
	Pagesize int   `json:"pagesize"`
}

func Arranges(ctx *gin.Context) {
	request := ArrangesBody{}
	ctx.ShouldBind(&request)
	state := request.State
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Arrange
	res := data_source.Db
	if state > 0 {
		res = res.Where("state = ?", state)
	}
	res.Model(&models.Arrange{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := ArrangesRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type BeliefBody struct {
	ID      int64  `json:"id" gorm:"id"`
	Name    string `json:"name" gorm:"name"`
	Content string `json:"content" gorm:"content"`
	State   int64  `json:"state" gorm:"state"`
	Value   int64  `json:"value" gorm:"value"`
}

func Arrangeadd(ctx *gin.Context) {
	request := BeliefBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	state := request.State
	belief := models.Arrange{
		Name:  name,
		State: state,
	}
	data_source.Db.Create(&belief)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Arrangeupdate(ctx *gin.Context) {
	request := BeliefBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	state := request.State
	belief := models.Arrange{
		Name:  name,
		State: state,
	}
	data_source.Db.Where("id = ?", id).Updates(&belief)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Arrangedel(ctx *gin.Context) {
	request := BeliefBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Arrange{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Biaoqians(ctx *gin.Context) {
	var admin []models.Biaoqian
	data_source.Db.Find(&admin)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    admin,
		"msg":     "ok",
	})
	return
}

func Biaoqianadd(ctx *gin.Context) {
	request := BeliefBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	content := request.Content
	belief := models.Biaoqian{
		Name:    name,
		Content: content,
	}
	data_source.Db.Create(&belief)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Biaoqianupdate(ctx *gin.Context) {
	request := BeliefBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	content := request.Content
	belief := models.Biaoqian{
		Name:    name,
		Content: content,
	}
	data_source.Db.Where("id = ?", id).Updates(&belief)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Biaoqiandel(ctx *gin.Context) {
	request := BeliefBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Biaoqian{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

type ThalicoinsRBody struct {
	Data       []models.Thalicoin `json:"data"`
	PageNo     int                `json:"pageNo"`
	PageSize   int                `json:"pageSize"`
	TotalCount int64              `json:"totalCount"`
	TotalPage  int64              `json:"totalPage"`
}

func Thalicoins(ctx *gin.Context) {
	request := ThalicoinsBody{}
	ctx.ShouldBind(&request)
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Thalicoin
	res := data_source.Db
	res.Model(&models.Thalicoin{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("price desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := ThalicoinsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type ThalicoinsBody struct {
	PageNo   int     `json:"pageNo"`
	Pagesize int     `json:"pagesize"`
	Price    float64 `json:"price" gorm:"price"`   // 金额
	Coin     float64 `json:"coin" gorm:"coin"`     // 平台币
	Jscoin   float64 `json:"jscoin" gorm:"jscoin"` // 首充加送平台币
	ID       int64   `json:"id" gorm:"id"`
	Num      int64   `json:"num" gorm:"num"`
}

func Thalicoinadd(ctx *gin.Context) {
	request := ThalicoinsBody{}
	ctx.ShouldBind(&request)
	price := request.Price
	coin := request.Coin
	jscoin := request.Jscoin
	num := request.Num
	belief := models.Thalicoin{
		Price:  price,
		Coin:   coin,
		Jscoin: jscoin,
		Num:    num,
	}
	data_source.Db.Create(&belief)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Thalicoinupdate(ctx *gin.Context) {
	request := ThalicoinsBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	price := request.Price
	coin := request.Coin
	jscoin := request.Jscoin
	num := request.Num
	belief := models.Thalicoin{
		Price:  price,
		Coin:   coin,
		Jscoin: jscoin,
		Num:    num,
	}
	data_source.Db.Where("id = ?", id).Updates(&belief)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Thalicoindel(ctx *gin.Context) {
	request := BeliefBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Thalicoin{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Configs(ctx *gin.Context) {

	var users []models.Config
	data_source.Db.Find(&users)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return
}
func Configupdate(ctx *gin.Context) {
	request := BeliefBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	value := request.Value

	data_source.Db.Model(&models.Config{}).Where("id = ?", id).Update("value", value)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
